<?php

class UserPageView {
	
	const max_message_characters = 400;
	
	/**
	 * @var NavigationView
	 */
	private $navView;
	
	/**
	 * Variables for keys.
	 * @var string
	 */
	private static $editPostKey 		= "editPost";
	private static $deletePostKey 		= "deletePost";
	private static $editCommentKey		= "editComment";
	private static $deleteCommentKey 	= "deleteComment";
	private static $beginEditPostKey	= "beginEditPost";
	private static $beginEditCommentKey = "beginEditComment";
	private static $addPostKey 			= "addPost";
	private static $addCommentKey 		= "addComment";
	private static $formImage	 		= "formImage";
	private static $formMessage		 	= "formString";
	private static $userIdURL			= "userId";
	private static $getPostId			= "getPostId";
	private static $getUserId			= "getUserId";
	private static $getCommentId		= "getCommentId";
	
	/**
	 * @param NavigationView $navView
	 */
	public function __construct(NavigationView $navView) {
		$this->navView = $navView;
	}
	
	/**
	 * -------------------------------------------------------------------------- User wants to...
	 * Functions that check what the user wants to do.
	 * @return boolean
	 */
	public function userWantsToAddPost() {
		return isset($_GET[self::$addPostKey]);
	}
	
	public function userWantsToDeletePost() {
		return isset($_GET[self::$deletePostKey]);
	}
	
	public function userWantsToEditPost() {
		return isset($_GET[self::$editPostKey]);
	}
	
	public function userWantsToAddComment() {
		return isset($_GET[self::$addCommentKey]);
	}
	
	public function userWantsToDeleteComment() {
		return isset($_GET[self::$deleteCommentKey]);
	}
	
	public function userWantsToEditComment() {
		return isset($_GET[self::$editCommentKey]);
	}
	
	public function userWantsToAddImage() {
		return is_uploaded_file($_FILES[self::$formImage]['tmp_name']);
	}
	
	/**
	 * -------------------------------------------------------------------------- Get form content
	 * Functions that return content from the forms.
	 * @var string
	 */
	public function getImageExtension() {
		if ($_FILES[self::$formImage]["error"] > 0) {
			throw new \Exception("Error: " . $_FILES["file"]["error"]);
	    }
		else {
			$_FILES[self::$formImage]["name"];
			$info = pathinfo($_FILES[self::$formImage]["name"]);
			return $info["extension"];
	    }
	}
	
	public function getImageTempName() {
		if ($_FILES[self::$formImage]["error"] > 0) {
			throw new \Exception("Error: " . $_FILES["file"]["error"]);
	    }
		else {
			return $_FILES[self::$formImage]["tmp_name"];
	    }
	}

	public function getMessage() {
		if(!empty($_POST[self::$formMessage])) {
			return nl2br(strip_tags($_POST[self::$formMessage]));
			
		}
		return "";
	}
	
	/**
	 * -------------------------------------------------------------------------- Get selected IDs
	 * Functions that return the selected id of ex. a post.
	 * @var string
	 */
	public function getSelectedPostId() {
		if (isset($_GET[self::$getPostId])) {
			return $_GET[self::$getPostId];
		}
		return "";
	}
	
	public function getSelectedUserId() {
		if (isset($_GET[self::$getUserId])) {
			return $_GET[self::$getUserId];
		}
		return "";
	}

	public function getSelectedCommentId() {
		if (isset($_GET[self::$getCommentId])) {
			return $_GET[self::$getCommentId];
		}
		return "";
	}
	
	/**
	 * @param string $username
	 * @param string $profilePicture
	 * @param string $posts HTML
	 * @param User	 $myUser
	 * 
	 * @var string $str HTML
	 * 
	 * @return string HTML
	 */
	public function getHTML($username, $profilePicture, $posts, User $myUser) {
		$str = "<section id='SideBar'>
					<img src='images/$profilePicture'></img>
					<h1>$username</h1>
				</section>
				<section id='Content'>";
				
		if (!isset($_GET[$this->navView->getUsernameURL()]) || $this->navView->getCurrentUsername() == $myUser->getUsername()) {
			$str .= $this->getPostFormHTML();
		}
			
		$str .= $posts .
				"</section>";
				
		return $str;
	}
	
	/**
	 * @return string HTML
	 */
	public function getPostFormHTML() {
		return "<button class='ShowPostForm'>Add a post</button>
				<div class='PostFormBox'>
					<form action='?".$this->navView->getUsernameURL()."=".$this->navView->getCurrentUsername().
								 "&". self::$addPostKey.
								 "' method='post' enctype='multipart/form-data' id='PostForm' class='Form'>
						<input type='file' name='". self::$formImage ."' onchange='fileSelected();'/>
						<textarea name='". self::$formMessage ."' placeholder='Write your post here...' maxlength='".self::max_message_characters."'></textarea>
						<button type='submit'>Submit</button>
						<button class='ClosePostForm' type='button'>Cancle</button>
					</form>
				</div>";
	}
	
	/**
	 * @param integer $postId
	 * 
	 * @return string HTML
	 */
	public function getCommentFormHTML($postId) {
		return "<button class='ShowCommentForm' value='CommentForm$postId'>Add a comment</button>
				<div id='CommentForm$postId' class='FormBox'>
					<form action='?".$this->navView->getUsernameURL()."=".$this->navView->getCurrentUsername().
								 "&".self::$addCommentKey.
								 "&".self::$getPostId."=$postId#PostId_$postId' method='post' class='Form'>
						<textarea name='". self::$formMessage ."' placeholder='Write your comment here...' maxlength='".self::max_message_characters."'></textarea>
						<button type='submit'>Submit</button>
						<button class='CloseCommentForm' value='CommentForm$postId' type='button'>Cancle</button>
					</form>
				</div>";
	}
	
	/**
	 * @param array 	$posts UserPost
	 * @param integer 	$userId
	 * 
	 * @var string		$str HTML
	 * 
	 * @return string 	HTML
	 */
	public function getPostsHTML($posts, $userId) {
		$str = "<section id='UserWall'>";
		
		foreach ($posts as $post) {
			if (isset($_GET[self::$beginEditPostKey]) && $this->getSelectedPostId() == $post->getPostId() && $userId == $this->getSelectedUserId()) {
				$str .= "<section class='UserPost' id='PostId_".$post->getPostId()."'>
							<h2>".$post->getUsername()."</h2>";
							if ($post->getImageString() != "") {
								$str .= "<img src='images/".$post->getImageString()."'></img>";
							}
							$str .= "<form action='?".$this->navView->getUsernameURL()."=".$this->navView->getCurrentUsername().
												  "&".self::$editPostKey.
												  "&".self::$getPostId."=".$post->getPostId().
												  "&".self::$getUserId."=".$post->getUserId().
												  "#PostId_".$post->getPostId().
												  "' method='post' enctype='multipart/form-data' class='Form'>
										 <input type='file' name='". self::$formImage ."' onchange='fileSelected();'/>
										 <textarea name='". self::$formMessage ."' maxlength='200'>". $post->getComment() ."</textarea>
										 <button type='submit'>Submit</button>
										 <a href='?".$this->navView->getUsernameURL().
												 "=".$this->navView->getCurrentUsername().
												 "'><button type='button'>Cancle</button></a>
									 </form>
									 <footer>". $post->getDateString() ."</footer>";
			}
			else {
				$str .= "<section class='UserPost' id='PostId_". $post->getPostId() ."'>";
				if ($userId == $post->getUserId()) {
					$str .= "<a class='Button' href='?".$this->navView->getUsernameURL()."=".$this->navView->getCurrentUsername().
													"&".self::$deletePostKey.
													"&".self::$getPostId."=".$post->getPostId().
													"&".self::$getUserId."=".$post->getUserId().
													"'>Delete</a>
							 <a class='Button' href='?".$this->navView->getUsernameURL()."=".$this->navView->getCurrentUsername().
												    "&".self::$beginEditPostKey.
												    "&".self::$getPostId."=".$post->getPostId().
												    "&".self::$getUserId."=".$post->getUserId().
												    "#PostId_".$post->getPostId().
												    "'>Edit</a>";
				}
				$str .=	"<h2>".$post->getUsername()."</h2>";
				if ($post->getImageString() != "") {
					$str .= "<img src='images/".$post->getImageString()."'></img>";
				}
				$str .= "<p>".$post->getComment()."</p>
					 	 <footer>".$post->getDateString()."</footer>";
			}
							
			$str .= $this->getCommentFormHTML($post->getPostId());
					
			foreach ($post->getCommentList() as $comment) {
				if (isset($_GET[self::$beginEditCommentKey]) && $this->getSelectedCommentId() == $comment->getCommentId() && $userId == $this->getSelectedUserId()) {
					$str .= "<section class='UserComment' id='CommentId_". $comment->getCommentId() ."'>
								<h3><a href='?".$this->navView->getUsernameURL()."=". $comment->getUsername() ."'>". $comment->getUsername() ."</a></h3>
								<form action='?".$this->navView->getUsernameURL()."=".$this->navView->getCurrentUsername().
											 "&".self::$editCommentKey.
											 "&".self::$getCommentId."=".$comment->getCommentId().
											 "&".self::$getUserId."=".$comment->getUserId().
											 "#CommentId_". $comment->getCommentId().
											 "' method='post' class='Form'>
									<textarea name='". self::$formMessage ."' maxlength='400'>". $comment->getComment() ."</textarea>
									<button type='submit'>Submit</button>
									<a href='?".$this->navView->getUsernameURL().
																  "=".$this->navView->getCurrentUsername().
																  "'><button type='button'>Cancle</button></a>
								</form>
								<footer>". $comment->getDateString() ."</footer>
							 </section>";
				}
				else {	
					$str .= "<section class='UserComment' id='CommentId_". $comment->getCommentId() ."'>";
					if ($userId == $comment->getUserId()) {
						$str .= "<a class='Button' href='?".$this->navView->getUsernameURL()."=".$this->navView->getCurrentUsername().
													    "&".self::$deleteCommentKey.
													    "&".self::$getCommentId."=".$comment->getCommentId().
													    "&".self::$getUserId."=".$comment->getUserId().
													    "'>Delete</a>
								 <a class='Button' href='?".$this->navView->getUsernameURL()."=".$this->navView->getCurrentUsername().
													    "&".self::$beginEditCommentKey.
													    "&".self::$getCommentId."=".$comment->getCommentId().
													    "&".self::$getUserId."=".$comment->getUserId().
													    "#CommentId_".$comment->getCommentId().
													    "'>Edit</a>";
					}
					$str .=	"<h3><a href='?".$this->navView->getUsernameURL()."=".$comment->getUsername()."'>".$comment->getUsername()."</a></h3>
							 <p>".$comment->getComment()."</p>
							 <footer>".$comment->getDateString()."</footer>
						  </section>";
				}
			}		
			$str .= "</section>";
			
		}
		return $str .= "</section>";
	}	
}